/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.unibo.as.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 *
 * @author Johnny
 */


public class SignupPanel extends DialogBox {

    private final VerticalPanel vp;
    private final Label errorLbl;


    public SignupPanel() {
        setText("New Account");

        vp = new VerticalPanel();
        vp.setWidth("400px");
        setWidget(vp);

        final FlexTable layout = new FlexTable();
        layout.setCellSpacing(6);
        final DecoratorPanel dp = new DecoratorPanel();

        final TextBox fnameBox = new TextBox();
        layout.setHTML(0, 0, "First Name: ");
        layout.setWidget(0, 1, fnameBox);

        final TextBox lnameBox = new TextBox();
        layout.setHTML(1, 0, "Last Name: ");
        layout.setWidget(1, 1, lnameBox);

        final TextBox emailBox = new TextBox();
        layout.setHTML(2, 0, "Email: ");
        layout.setWidget(2, 1, emailBox);

        final TextBox userBox = new TextBox();
        layout.setHTML(3, 0, "Nickname: ");
        layout.setWidget(3, 1, userBox);

        final PasswordTextBox passBox = new PasswordTextBox();
        layout.setHTML(4, 0, "Password: ");
        layout.setWidget(4, 1, passBox);

        PasswordTextBox passBox2 = new PasswordTextBox();
        layout.setHTML(5, 0, "Re-digit Password: ");
        layout.setWidget(5, 1, passBox2);

        final Button signUpBtn = new Button("SignUp");
        layout.setWidget(6,0, signUpBtn);

        final Button cancelBtn = new Button("Cancel");
        layout.setWidget(6,1, cancelBtn);

        errorLbl = new Label();
        

        final Button okBtn = new Button("OK");
        okBtn.setVisible(false);


        cancelBtn.addClickHandler(new ClickHandler(){
        @Override
            public void onClick(ClickEvent event) {
                hide();
            }
        });

        okBtn.addClickHandler(new ClickHandler(){
        @Override
            public void onClick(ClickEvent event) {
                hide();
            }
        });

        signUpBtn.addClickHandler(new ClickHandler(){

            final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean result) {
                    if(result){
                        errorLbl.setText("Registration complete.");
                        dp.setVisible(false);
                        okBtn.setVisible(true);
                    }
                    else{
                        /* On failure, set error label */
                        errorLbl.setText("An error occured.");
                    }

                }

                @Override
                public void onFailure(Throwable caught) {
                    errorLbl.setText("Communication failed");
                }
            };
        
            @Override
            public void onClick(ClickEvent event) {
            /* TODO: add field validation */

            getSignupService().signUp(lnameBox.getText(), fnameBox.getText(),userBox.getText(),emailBox.getText(),passBox.getText(), callback);

            }
        });

        

        dp.setWidget(layout);
        vp.add(dp);
        vp.add(errorLbl);
        vp.add(okBtn);


    }


    public static GWTAccountServiceAsync getSignupService() {
        // Create the client proxy. Note that although you are creating the
        // service interface proper, you cast the result to the asynchronous
        // version of the interface. The cast is always safe because the
        // generated proxy implements the asynchronous interface automatically.

        return GWT.create(GWTAccountService.class);
    }

}
